<?php
class mod_user extends core_modbase {

	protected $auth;
	protected $session;
	protected $user;
	protected $menu;
	
	public function __construct()
	{
		parent::__construct();
		$this->auth = new auth();
		$this->session = new session();
		$this->user = new user();
	}
	
	public function PAGE_panel ()
	{
		$args = func_get_args();
		return call_user_func_array(array($this, 'PAGE_index'), $args);
	}
	
	public function SetMenu ($menu)
	{
		$this->menu = array_merge_recursive($this->menu, $menu);
	}

	protected function MakeMenu($menu)
	{
		$return = array();
		foreach ($menu as $name => $item)
		{
			if ($this->translator->get($name))
				$name = $this->translator->get($name);
			$item = str_replace('{SP}', core_settings::Get('sitepath'), $item);
			$item = str_replace('{UP}', core_settings::Get('sitepath') . 'user/panel/', $item);
			$return[$name] = $item;
		}
		return $return;
	}
	
	public function PAGE_index ($module = 'user', $function = 'index')
	{
		if (!$this->user->isRegistred())
			throw new Error404();
		
		$this->menu = array();
		core_signal::SetHandle('user::menu', array($this, 'SetMenu'));
		
		$mods = core_modules::FindFile('user.php');
		foreach ($mods as $mod)
		{
			require MODSDIR . core_modules::modDir($mod) . DIRECTORY_SEPARATOR . 'user.php';
			$mod = substr($mod, 4);
			$class = 'usr_' . $mod;
			$instances[$mod] = new $class('user');
		}

		if (isset($instances[$module]) && method_exists($instances[$module], 'PAGE_' . $function))
		{
			$arg = func_get_args();
			unset($arg[1], $arg[0]);
			
			$ret = call_user_func_array(array($instances[$module], 'PAGE_' . $function), $arg);
			if ($ret !== null)
			{
				$this->translator->load('user', 'mod_' . $module);
				$this->template->assign('Menu', $this->MakeMenu($this->menu));
				$this->template->assign('subMenu', $this->MakeMenu(call_user_func(array($instances[$module], 'submenu'))));
				$this->template->assign('page', $ret);
				$this->display('pannel.html');
			}
			else
				throw new Error404();
		}
		else
		{
			throw new Error404();
		}
	}
	
	public function PAGE_inscription ($etape = 'log_info')
	{
		$this->translator->load('inscription');
		$captcha = new Captcha();
		$auth_form = $this->auth->InscriptionForm($this->translator);
		$user_form = $this->user->InscriptionForm($this->translator);

		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			try 
			{
				if ($auth_form->valid($_POST) && $user_form->valid($_POST) && $captcha->verify($_POST['code']))
				{
					$user_id = $this->user->create($_POST);
					$this->auth->create($user_id, $_POST);
					$this->session->set('registred_user', $user_id);
					$this->assign('message', 'inscrit');
					$this->display('message.html');
					return;
				}
			}
			catch (FormValidateError $e)
			{
				$this->template->assign('message', $e->getMessage());
			}
		}
		
		$this->template->assign('auth_form', $auth_form->Generate($_POST));
		$this->template->assign('detail_form', $user_form->Generate($_POST));
		$this->template->assign('CAPTCHA', $captcha->display());
		$this->display('inscription.html');
	}
	
	public function PAGE_deconnexion ()
	{
		$this->assign('message', 'logout');
		$this->session->delete('registred_user');
		$this->display('message.html');
	}
	
	public function PAGE_connexion ()
	{
		if ($this->user->isRegistred())
		{
			$this->assign('message', 'already_connected');
			$this->display('message.html');
		}
		elseif ($this->auth->isPosted())
		{
			try {
				$user_id = $this->auth->Connect();
				$this->session->set('registred_user', $user_id);
				$this->assign('message', 'connected');
				$this->display('message.html');
			}
			catch (LoginError $e)
			{
				$this->assign('message', 'mdperror');
				$this->template->assign('connectform', $this->auth->GetForm());
				$this->display('connexion.html');
			}
		}
		else
		{
			$this->template->assign('connectform', $this->auth->GetForm());
			$this->display('connexion.html');
		}
	}

}
